﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScannerSuccessRate.PLC
{
    public class MyPlc : Plc
    {
        public MyPlc(CpuType cpu, string ip, short rack, short slot) : base(cpu, ip, rack, slot)
        {
        }

        public MyPlc(CpuType cpu, string ip, int port, short rack, short slot) : base(cpu, ip, port, rack, slot)
        {
        }
        public new void WriteClass(object classValue, int db, int startByteAdr = 0)
        {
            rw(false, classValue, db, startByteAdr);
        }
        public new int ReadClass(object sourceClass, int db, int startByteAdr = 0)
        {
            return rw(true, sourceClass, db, startByteAdr);
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        private int rw(bool isRead, object sourceClass, int db, int startByteAdr = 0)
        {
            Plc plc = this as Plc;
            try
            {
                if (isRead)
                {
                    return plc.ReadClass(sourceClass, db, startByteAdr);
                }
                else
                {
                    plc.WriteClass(sourceClass, db, startByteAdr);
                    return -2;
                }
            }
            catch(Exception ex)
            {
                //MessageBox.Show(ex.Message);
                return -100;
            }
        }
    }
}
